package com.xbongbong.crm.service.condition;

import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.parent.abstracts.AbstractConditionService;
import com.xbongbong.parent.pojo.list.ConditionListPojo;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/06/15 16:37
 */
@Service("clueConditionServiceImpl")
public class ClueConditionServiceImpl extends AbstractConditionService {

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CLUE.getCode(), XbbRefTypeEnum.ALL_CLUE.getCode(), XbbRefTypeEnum.CLUE_PUBLIC.getCode());
    }

    @Override
    public List<ConditionsEntityExt> packageConditionBySubBusinessType(ConditionListPojo conditionListPojo) {
        if (Objects.equals(XbbRefTypeEnum.CLUE_RECYCLE.getCode(), conditionListPojo.getSubBusinessType())) {
            // 回收站不需要调用这个方法
            return new ArrayList<>();
        }
        List<ConditionsEntityExt> conditionList = new ArrayList<>();
        Integer isPublic = 0;
        Integer subBusinessType = conditionListPojo.getSubBusinessType();
        if (Objects.equals(subBusinessType, XbbRefTypeEnum.CLUE_PUBLIC.getCode())) {
            isPublic = 1;
        }
        ConditionsEntityExt conditionsEntityExt = new ConditionsEntityExt();
        conditionsEntityExt.setAttr(ClueEnum.IS_PUBLIC.getAttr());
        conditionsEntityExt.setFieldType(ClueEnum.IS_PUBLIC.getFieldType());
        conditionsEntityExt.setName(ClueEnum.IS_PUBLIC.getAttrName());
        conditionsEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
        conditionsEntityExt.setValue(Collections.singletonList(isPublic));
        conditionList.add(conditionsEntityExt);
        Integer statusFilter = conditionListPojo.getStatusFilter();
        boolean isArchived = !Objects.equals(subBusinessType,XbbRefTypeEnum.CLUE_PUBLIC.getCode()) && !(Objects.isNull(statusFilter) || statusFilter == 0);
        if (isArchived){
            ConditionsEntityExt statusFilterEntityExt = new ConditionsEntityExt();
            statusFilterEntityExt.setAttr(ClueEnum.IS_ARCHIVED.getAttr());
            statusFilterEntityExt.setFieldType(ClueEnum.IS_ARCHIVED.getFieldType());
            statusFilterEntityExt.setSymbol(ConditionEnum.EQUAL.getSymbol());
            statusFilterEntityExt.setValue(Collections.singletonList(statusFilter));
            conditionList.add(statusFilterEntityExt);
        }
        return conditionList;
    }
}
